package com.bootdo.system.service.impl;

import com.bootdo.common.domain.DictDO;
import com.bootdo.common.utils.DictUtils;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.StringUtils;
import com.bootdo.system.dao.*;
import com.bootdo.system.domain.*;
import com.bootdo.system.service.GuessLogService;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Transactional
public class GuessLogServiceImpl implements GuessLogService {

	@Autowired
	private GuessLogDao guessLogDao;

	@Autowired
	private GuessItemDao guessItemDao;

	@Autowired
	private UserDao userDao;

	@Autowired
	private GuessDao guessDao;

	@Autowired
	private UserRoleDao userRoleDao;

	@Autowired
	private JournalDao journalDao;

	@Override
	public GuessLogDO get(Long id){
		return guessLogDao.get(id);
	}
	
	@Override
	public List<GuessLogDO> list(Map<String, Object> map){
		return guessLogDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return guessLogDao.count(map);
	}
	
	@Override
	public int update(GuessLogDO log){
		return guessLogDao.update(log);
	}
	
	@Override
	public int remove(Long id){
		return guessLogDao.remove(id);
	}
	
	@Override
	public int batchRemove(Long[] ids){
		return guessLogDao.batchRemove(ids);
	}

	@Override
	public R save(GuessLogDO log,GuessItemDO guessItemDO) {
		//获取竞猜扣除积分比率
		/*DictDO dictDO = DictUtils.getDictByType("give_rate");
		if(null == dictDO) {
			return R.error("竞猜积分扣除比率不能为空");
		}
		float giveRate = 0;
		giveRate = Float.parseFloat(dictDO.getValue());
		BigDecimal rate = new BigDecimal(giveRate / 100);
		BigDecimal total = new BigDecimal(log.getIntegral()).multiply(rate);
		Long number = Math.round(total.doubleValue());
		long total1 = number + log.getIntegral();*/
		GuessDO guessDO = guessDao.get(log.getGuessId());
		//判断积分是否充足
		UserDO userDO = userDao.get(log.getCreateBy());
		//获取下注人用户角色（普通用户下注，需要扣除积分，管理员下注，不需要扣除本身积分）
		List<Long> list = userRoleDao.listRoleId(log.getCreateBy());
		if(!CollectionUtils.isEmpty(list)) {
			if(!list.contains(1l)) {
				if(StringUtils.equals("1",userDO.getFrozenType())) {
					return R.error("赠送人积分账号已冻结");
				}
				if(log.getIntegral() > userDO.getIntegral()) {
					return R.error("当前您的积分不足");
				}
				//积分余额
				log.setAllIntegral(userDO.getIntegral() - log.getIntegral());
			}else {
				//积分余额
				log.setAllIntegral(userDO.getIntegral());
			}
		}

		//保存下注参与记录
		log.setCreateDate(new Date());
		log.setDelFlag("0");
		log.setGuessDescription("竞猜活动下注" + log.getIntegral() + "积分");
		log.setShowId(guessDO.getShowId());
		guessLogDao.save(log);

		//保存积分流水
		String strDateFormat = "yyyy-MM-dd HH:mm:ss";
		SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
		String startDate = sdf.format(guessDO.getGuessStartDate());
		String endDate = sdf.format(guessDO.getGuessEndDate());
		JournalDO journalDO1 = new JournalDO();
		journalDO1 = setData(userDO);
		journalDO1.setJournalType("2");
		journalDO1.setJournalDescription("竞猜活动下注" + log.getIntegral() + "积分");
		journalDO1.setIntegral(log.getIntegral());
		journalDO1.setServiceIntegral(0l);
		journalDO1.setAllIntegral(log.getAllIntegral());
		journalDO1.setShowId(guessDO.getShowId());
		journalDO1.setGuessDetail("竞猜活动为开始时间为" + startDate + ",结束时间为" + endDate + "的" + guessDO.getGuessTitle() + "活动");
		journalDao.save(journalDO1);

		//用户扣除积分
		if(!CollectionUtils.isEmpty(list)) {
			if(!list.contains(1l)) {
				userDO.setIntegral(log.getIntegral());
				userDao.reduceIntegral(userDO);
			}
		}
		//更新竞猜项目下注总积分
		guessItemDO.setId(log.getAnswerId());
		guessItemDO.setGuessIntegral(log.getIntegral());
		guessItemDao.addIntegral(guessItemDO);
		//获取竞猜项目
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("guessId",guessItemDO.getGuessId());
		List<GuessItemDO> list1 = guessItemDao.list(map);
		//计算竞猜赔率
		double guessOdds = 1.95;
		GuessItemDO guessItemDO1 = list1.get(0);
		GuessItemDO guessItemDO2 = list1.get(1);
		try {
			double guessOdds1 = new BigDecimal(Double.valueOf(guessItemDO1.getGuessIntegral())/Double.valueOf(guessItemDO2.getGuessIntegral())).doubleValue();
			double guessOdds2 = new BigDecimal(Double.valueOf(guessItemDO2.getGuessIntegral())/Double.valueOf(guessItemDO1.getGuessIntegral())).doubleValue();
			guessItemDO1.setGuessOdds(guessOdds1);
			guessItemDO2.setGuessOdds(guessOdds2);
			if(guessOdds == 1) {
				guessOdds = 1.95;
				guessItemDO1.setGuessOdds(guessOdds);
				guessItemDO2.setGuessOdds(guessOdds);
			}
		}catch (Exception e) {
			guessOdds = 1.95;
			guessItemDO1.setGuessOdds(guessOdds);
			guessItemDO2.setGuessOdds(guessOdds);
		}
		List<GuessItemDO> list2 = new ArrayList<GuessItemDO>();
		list2.add(guessItemDO1);
		list2.add(guessItemDO2);
		map.put("guessItemDOList",list2);
		return R.ok(map);
	}

	/**
	 * 竞猜开奖
	 * @param guessLogDO
	 * @param guessItemDO
	 * @return
	 */
	@Override
	public R drawPrize(GuessLogDO guessLogDO, GuessItemDO guessItemDO) {
		//获取竞猜扣除积分比率
		DictDO dictDO = DictUtils.getDictByType("give_rate");
		if(null == dictDO) {
			return R.error("竞猜积分扣除比率不能为空");
		}
		float giveRate = 0;
		giveRate = Float.parseFloat(dictDO.getValue());
		BigDecimal rate = new BigDecimal(giveRate / 100);
		//获取竞猜信息
		GuessDO guessDO = guessDao.get(guessLogDO.getGuessId());
		//获取竞猜项目
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("guessId",guessLogDO.getGuessId());
		List<GuessItemDO> list = guessItemDao.list(map);
		//计算竞猜赔率
		double guessOdds = 1.95;
		GuessItemDO guessItemDO1 = list.get(0);
		GuessItemDO guessItemDO2 = list.get(1);
		//获取竞猜记录
		map.put("showId",guessDO.getShowId());
		List<GuessLogDO> guessLogList = guessLogDao.list(map);
		if(!CollectionUtils.isEmpty(guessLogList)) {
			UserDO userDO = null;
			String strDateFormat = "yyyy-MM-dd HH:mm:ss";
			SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
			String startDate = sdf.format(guessDO.getGuessStartDate());
			String endDate = sdf.format(guessDO.getGuessEndDate());
			for(GuessLogDO data : guessLogList) {
				//获取账户积分余额
				userDO = new UserDO();
				userDO.setUserId(data.getCreateBy());
				UserDO userDO1 = userDao.get(userDO.getUserId());
				if(data.getAnswerId() == guessDO.getAnswerId()) {
					//中奖
					try{
						if(guessItemDO1.getId() == data.getAnswerId()) {
							guessOdds = new BigDecimal(Double.valueOf(guessItemDO1.getGuessIntegral())/Double.valueOf(guessItemDO2.getGuessIntegral())).doubleValue();
						}else {
							guessOdds = new BigDecimal(Double.valueOf(guessItemDO2.getGuessIntegral())/Double.valueOf(guessItemDO1.getGuessIntegral())).doubleValue();
						}
						if(guessOdds == 1) {
							guessOdds = 1.95;
						}else {
							guessOdds = guessOdds + 1;
						}
					}catch (Exception e) {
						guessOdds = 1.95;
					}
					//计算中奖人员获得的积分
					BigDecimal total = new BigDecimal(data.getIntegral()).multiply(new BigDecimal(guessOdds));
					Long total1 = Math.round(total.doubleValue());
					//用户增加积分
					userDO.setIntegral(total1);
					userDao.addIntegral(userDO);

					//保存积分流水
					JournalDO journalDO1 = new JournalDO();
					journalDO1 = setData(userDO1);
					journalDO1.setJournalType("3");
					journalDO1.setJournalDescription("开奖中奖获得" + total1 + "积分");
					journalDO1.setIntegral(total1);
					journalDO1.setServiceIntegral(0l);
					journalDO1.setAllIntegral(userDO1.getIntegral() + total1);
					journalDO1.setShowId(guessDO.getShowId());
					journalDO1.setGuessDetail("竞猜活动为开始时间为" + startDate + ",结束时间为" + endDate + "的" + guessDO.getGuessTitle() + "活动");
					journalDao.save(journalDO1);

					//更新竞猜信息
					//0.正确 1.错误 2.流局
					data.setGuessResult("0");
					data.setFinalIntegral(total1);
					data.setAllIntegral(userDO1.getIntegral() + total1);
					data.setGuessDescription(data.getGuessDescription() + ";开奖后中奖获得的积分为" + total1 + "积分");
					guessLogDao.update(data);
				}else {
					//不中奖
					//更新竞猜信息
					//0.正确 1.错误 2.流局
					//计算手续费
					BigDecimal total = new BigDecimal(data.getIntegral()).multiply(rate);
					int serviceIntegral = (int)Math.round(total.doubleValue());
					data.setGuessResult("1");
					data.setFinalIntegral(data.getIntegral());
					data.setAllIntegral(userDO1.getIntegral());
					data.setGuessDescription(data.getGuessDescription() + ";开奖后未中奖扣除手续费积分" + serviceIntegral + "积分");
					guessLogDao.update(data);

					//保存积分流水
					JournalDO journalDO1 = new JournalDO();
					journalDO1 = setData(userDO1);
					journalDO1.setJournalType("4");
					journalDO1.setJournalDescription("开奖后未中奖扣除手续费积分" + serviceIntegral + "积分");
					journalDO1.setIntegral(data.getIntegral());
					journalDO1.setServiceIntegral(Long.valueOf(serviceIntegral));
					journalDO1.setAllIntegral(userDO1.getIntegral());
					journalDO1.setShowId(guessDO.getShowId());
					journalDO1.setGuessDetail("竞猜活动为开始时间为" + startDate + ",结束时间为" + endDate + "的" + guessDO.getGuessTitle() + "活动");
					journalDao.save(journalDO1);
				}
			}
		}
		//修改开奖活动状态
		guessDO.setGuessState("3");//已结束
		guessDao.update(guessDO);
		return R.ok().put("answerName",guessItemDO.getGuessName());
	}

	private JournalDO setData(UserDO userDO) {
		JournalDO journalDO = new JournalDO();
		journalDO.setCreateBy(userDO.getUserId());
		journalDO.setUserId(userDO.getUserId());
		journalDO.setCreateDate(new Date());
		journalDO.setDelFlag("1");
		return journalDO;
	}

	public static void main(String[] args) {
		long a = 4000;
		long b = 5000;
		BigDecimal c = new BigDecimal(Double.valueOf(a - a * 0.05)/Double.valueOf(b - b * 0.05));
		double d = c.doubleValue();
		System.out.println(c);
		System.out.println(d);
	}

}
